﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfPanel
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;
        }

        bool isChecked = false;

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            BlockWindow window = new BlockWindow();
            window.LoadResource();

            Label btnLabel = new Label();
            Viewbox vb = new Viewbox();
            vb.Stretch = Stretch.Uniform;
            vb.Child = btnLabel;
            btnLabel.Template = window.ControlTemplate;
            butt.Content = vb;

            cControl = new ContentControl();
            Label icon = new Label();
            Viewbox vbIcon = new Viewbox();
            vbIcon.Stretch = Stretch.Uniform;
            vbIcon.Child = icon;
            icon.Template = window.ControlTemplateIcon;
            cControl.Content = vbIcon;
            //cControl.Width = 26;
            cControl.Width = 100;
            

            border = new Rectangle();
            border.Width = 100;
            border.Height = 100;
            border.Fill = border.Stroke = Brushes.Pink;
            canvas.Children.Add(border);
            canvas.Children.Add(cControl);
        }

        Rectangle border;
        ContentControl cControl;

        private void butt_Click(object sender, RoutedEventArgs e)
        {
            isChecked = butt.IsChecked ?? false;

            if (isChecked)
            {
                cControl.Visibility = System.Windows.Visibility.Visible;
            }
            else
	        {
                cControl.Visibility = System.Windows.Visibility.Hidden;
	        }  
        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isChecked) return;

            Point p = e.GetPosition(canvas);

            double nearestDistance = Double.MaxValue;
            Point nearestPoint = new Point();
            Point nearestPoint2 = new Point();
            Point intersect = new Point();

            Point last = poly.Points[0];
            for (int i = 1; i <= poly.Points.Count; i++)
            {
                Point point;
                if (i != poly.Points.Count)
                {
                    point = poly.Points[i];
                }
                else
                {
                    point = poly.Points[0];
                }

                Point temp;
                double dist = 0;
                if ((dist = LineToPointDistance(last, point, p, out temp)) < nearestDistance)
                {
                    intersect = temp;
                    nearestDistance = dist;
                    nearestPoint = last;
                    nearestPoint2 = point;
                }

                last = point;
            }

            if (nearestDistance > 5000)
            {
                intersect.X = -5;
                intersect.Y = -5;
            }

            Vector lOne = new Vector(nearestPoint2.X - nearestPoint.X, nearestPoint2.Y - nearestPoint.Y);
            Vector lTwo = new Vector(10, 0);
            double angl = Vector.AngleBetween(lTwo, lOne);
            //Vector.AngleBetween(new Vector(nearestPoint, nearestPoint2), new Vector(

            Canvas.SetLeft(ellipsePoint, intersect.X - 5);
            Canvas.SetTop(ellipsePoint, intersect.Y - 5);

            Canvas.SetLeft(border, (intersect.X ));
            Canvas.SetTop(border, (intersect.Y ));

            double x = Math.Sin(angl) * 50.0;
            double y = Math.Cos(angl) * 26.0;
            
            Canvas.SetLeft(cControl, intersect.X - x);
            Canvas.SetTop(cControl, intersect.Y - y);

            cControl.RenderTransform = new RotateTransform(angl);
        }

        /// <summary>
        /// Lines to point distance.
        /// http://pastebin.com/CbMrH48e
        /// </summary>
        /// <param name="linePoint1">The line point1.</param>
        /// <param name="linePoint2">The line point2.</param>
        /// <param name="point">The point.</param>
        /// <returns></returns>
        private double LineToPointDistance(Point linePoint1, Point linePoint2, Point point, out Point intersect)
        {

            double u =
                    ((point.X - linePoint1.X) * (linePoint2.X - linePoint1.X) +
                    (point.Y - linePoint1.Y) * (linePoint2.Y - linePoint1.Y)) /
                    (linePoint2 - linePoint1).LengthSquared;

            intersect = new Point(
                linePoint1.X + u * (linePoint2.X - linePoint1.X),
                linePoint1.Y + u * (linePoint2.Y - linePoint1.Y)
            );

            if (Math.Min(linePoint1.X, linePoint2.X) <= intersect.X && intersect.X <= Math.Max(linePoint1.X, linePoint2.X)
                && Math.Min(linePoint1.Y, linePoint2.Y) <= intersect.Y && intersect.Y <= Math.Max(linePoint1.Y, linePoint2.Y))
            {
                Vector distance = Point.Subtract(intersect, point);

                // distance.X = Math.Abs(distance.X);
                // distance.Y = Math.Abs(distance.Y);

                return distance.LengthSquared;
            }
            else
            {
                return double.MaxValue;
            }
        }
    }
}
